package rfc8009import ()const ( s2kParamsZero = 32768)// DeriveRandom for key derivation as defined in RFC 8009func (, []byte, etype.EType) ([]byte, error) { := .GetHashFunc()()returnKDF_HMAC_SHA2(, []byte("prf"), , .Size(), ), nil}// DeriveKey derives a key from the protocol key based on the usage and the etype's specific methods.//// https://tools.ietf.org/html/rfc8009#section-5func (, []byte, etype.EType) []byte {var []bytevarint// Key length is longer for aes256-cts-hmac-sha384-192 is it is a Ke or from StringToKey (where label is "kerberos")if .GetETypeID() == etypeID.AES256_CTS_HMAC_SHA384_192 { :switch [len()-1] {case0x73:// 0x73 is "s" so label could be kerberos meaning StringToKey so now check if the label is "kerberos" := []byte("kerberos")iflen() != len() {break }for , := range {if != [] { = .GetKeySeedBitLength()break } }if == 0 {// This is StringToKey = 256 }case0xAA:// This is a Ke = 256 } }if == 0 { = .GetKeySeedBitLength() }return .RandomToKey(KDF_HMAC_SHA2(, , , , ))}// RandomToKey returns a key from the bytes provided according to the definition in RFC 8009.func ( []byte) []byte {return}// StringToKey returns a key derived from the string provided according to the definition in RFC 8009.func (, , string, etype.EType) ([]byte, error) { , := S2KparamsToItertions()if != nil {returnnil, }returnStringToKeyIter(, , , )}// StringToKeyIter returns a key derived from the string provided according to the definition in RFC 8009.func (, string, int, etype.EType) ([]byte, error) { := .RandomToKey(StringToPBKDF2(, , , ))return .DeriveKey(, []byte("kerberos"))}// StringToPBKDF2 generates an encryption key from a pass phrase and salt string using the PBKDF2 function from PKCS #5 v2.0func (, string, int, etype.EType) []byte { := .GetKeyByteSize()if .GetETypeID() == etypeID.AES256_CTS_HMAC_SHA384_192 { = 32 }returnpbkdf2.Key([]byte(), []byte(), , , .GetHashFunc())}// KDF_HMAC_SHA2 key derivation: https://tools.ietf.org/html/rfc8009#section-3func (, , []byte, int, etype.EType) []byte {//k: Length in bits of the key to be outputted, expressed in big-endian binary representation in 4 bytes. := make([]byte, 4, 4)binary.BigEndian.PutUint32(, uint32()) := make([]byte, 4, 4)binary.BigEndian.PutUint32(, uint32(1)) = append(, ...) = append(, byte(0))iflen() > 0 { = append(, ...) } = append(, ...) := hmac.New(.GetHashFunc(), ) .Write()return .Sum(nil)[:( / 8)]}// GetSaltP returns the salt value based on the etype name: https://tools.ietf.org/html/rfc8009#section-4func (, string) string { := []byte() = append(, byte(0)) = append(, []byte()...)returnstring()}// S2KparamsToItertions converts the string representation of iterations to an integer for RFC 8009.func ( string) (int, error) {varuint32iflen() != 8 {returns2kParamsZero, errors.New("Invalid s2kparams length") } , := hex.DecodeString()if != nil {returns2kParamsZero, errors.New("Invalid s2kparams, cannot decode string to bytes") } = binary.BigEndian.Uint32()//buf := bytes.NewBuffer(b) //err = binary.Read(buf, binary.BigEndian, &i)if != nil {returns2kParamsZero, errors.New("Invalid s2kparams, cannot convert to big endian int32") }returnint(), nil}
The pages are generated with Goldsv0.6.7. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds.